package com.example.enterprisemanager.mvvm.fragment.manager.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.example.enterprisemanager.R;

import java.util.Objects;

public class MyDialogInput extends DialogFragment {

    private String title;
    public OnDialogGetContext onBtnGetEdit;

    public void show(String title, OnDialogGetContext onBtnGetEdit, FragmentManager fragmentManager) {
        this.title = title;
        this.onBtnGetEdit = onBtnGetEdit;
        show(fragmentManager, "ButtonDialogFragment");
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_dialog_input, null);
        EditText editText = view.findViewById(R.id.edt_num_dialog);

        view.findViewById(R.id.btn_ok_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBtnGetEdit.getEditContext(editText.getText().toString());
                dismiss();
            }
        });

        view.findViewById(R.id.btn_cancel_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        return new AlertDialog.Builder(Objects.requireNonNull(getActivity()))
                .setView(view)
                .setTitle(title)
                .create();
    }
}
